package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.secret.SecretAESDESede;
import com.zcx.helper.util.UtilApp;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import longcaisuyun.longcai.com.mystatic.publicUrl;
import longcaisuyun.longcai.com.net.PostBanBen;

//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//更多的页面

/************************************************************/

public class MoreActivity extends Activity implements View.OnClickListener {
    LinearLayout lin_jiancha, lin_shoufei, lin_xieyi, fanhui_lin;
    private Dialog dialog;
    private String url;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        ViewInit();
    }

    private void ViewInit() {
        //点击提示dialog
        lin_jiancha = (LinearLayout) findViewById(R.id.lin_jiancha);
        lin_shoufei = (LinearLayout) findViewById(R.id.lin_shoufei);
        lin_xieyi = (LinearLayout) findViewById(R.id.lin_xieyi);
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lin_jiancha.setOnClickListener(this);
        lin_shoufei.setOnClickListener(this);
        lin_xieyi.setOnClickListener(this);
        fanhui_lin.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui_lin:
                MoreActivity.this.finish();
                break;
            case R.id.lin_jiancha:
//                PostHttp();
                PostHttpVersion();
                break;
            case R.id.lin_shoufei:
                startActivity(new Intent(MoreActivity.this, ShouFeiActivity.class));
                break;
            case R.id.lin_xieyi:
                startActivity(new Intent(MoreActivity.this, More_Sijixieyi_activity.class));
                break;
        }
    }

    private void PostHttp() {
        MyApplication.myviewutil.startProgressDialog(MoreActivity.this);
        final String versionName = UtilApp.versionName(this);
        new PostBanBen(versionName,new AsyCallBack<PostBanBen.Info>(){
            @Override
            public void onSuccess(String toast, int type, PostBanBen.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                MyApplication.myviewutil.stopProgressDialog();
                if (info.message.equals("1")){
                    if (info.web_d.equals(versionName)){
                        Toast.makeText(MoreActivity.this, "当前已经是最新版本", Toast.LENGTH_SHORT).show();
                    }else{
                        url=info.url;
                        showMyDialog();
                    }
                }else{
                    Toast.makeText(MoreActivity.this, "获取版本号失败，请稍后重试", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                MyApplication.myviewutil.stopProgressDialog();
                Toast.makeText(MoreActivity.this, "网络故障，请稍后重试", Toast.LENGTH_SHORT).show();
            }
        }).execute(MoreActivity.this);
    }
    //更新doalog
    private void showMyDialog() {
            LayoutInflater inflater = LayoutInflater.from(this);
            LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.item_dingdan_phone,
                    null);
            MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
            dialog = new Dialog(this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
            dialog.show();
            dialog.setContentView(layout);
            TextView textView29 = (TextView) layout.findViewById(R.id.textView29);
            textView29.setVisibility(View.GONE);
            TextView textView28 = (TextView) layout.findViewById(R.id.textView28);
            textView28.setText("您有新版本需要更新");
            TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
            tv_phone.setText("更新");
            TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
            tv_phone.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    Intent intent=new Intent(MoreActivity.this,RegardingService.class);
                    intent.putExtra("url",url);
                    startService(intent);
                    dialog.dismiss();
                }
            });
            tv_cancle.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
    }

    private void PostHttpVersion(){
        final SecretAESDESede secretAESDESede = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.DESEDE_CBC_PKCS5PADDING);
        final FinalHttp finalHttp =new FinalHttp();
        AjaxParams ajaxParams=new AjaxParams();
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("version","1.0");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("post传:", jsonObject.toString());
        try {
            ajaxParams.put("Keys", secretAESDESede.encrypt(jsonObject.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        finalHttp.post(publicUrl.banBenHao, ajaxParams,new AjaxCallBack<String>() {
            @Override
            public void onStart() {
                super.onStart();
                MyApplication.myviewutil.startProgressDialog(MoreActivity.this);
            }

            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                MyApplication.myviewutil.stopProgressDialog();
                try {
                    Log.e("接值",secretAESDESede.decrypt(o).toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JSONObject jsonObject1 = null;
                try {
                    try {
                        jsonObject1=new JSONObject(secretAESDESede.decrypt(o));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (jsonObject1.getString("message").equals("1")){
                        if (jsonObject1.getString("web_d").equals("1.0")){
                            Toast.makeText(MoreActivity.this, "当前已经是最新版本 2.01", Toast.LENGTH_SHORT).show();
                        }else{
                            url=jsonObject1.getString("url");
                            showMyDialog();
                        }
                    }else if (jsonObject1.getString("message").equals("0")) {
                        Toast.makeText(MoreActivity.this, "获取版本号失败，请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                Toast.makeText(MoreActivity.this, "网络故障，请稍后重试", Toast.LENGTH_SHORT).show();
                MyApplication.myviewutil.stopProgressDialog();
            }
        });
    }
}
